﻿using System.Collections.Generic;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace UFrame
{
    internal enum ObjectType
    {
        None,
        Monster,
        Boss,
        Role,
    }

    /// <summary>
    /// 特效总界面
    /// </summary>
    public class EffectEditor : EditorWindow
    {

        public Object obj = null;

        public GameObject createdObject = null;

        public SkillEffectEditorConfigManager config = null;

        public bool isPlaying = false;

        public AnimatorEditorPlayer meshPlayer = null;

        List<SkillSecondWindow> windowList = new List<SkillSecondWindow>();

        public Dictionary<string, Transform> effectBones = new Dictionary<string, Transform>();

        public List<EditorParticlePlayer> effectPlayer = new List<EditorParticlePlayer>();

        public PLSkillInfoWindow controlWindow = null;

        public SkillListWindow highSkillListWindow = null;

        public SkillListWindow lowSkillListWindow = null;

        public EffectListWindow effectListWindow = null;

        public EffectInfoWindow effectInfoWindow = null;

        public GameObject attackTarget = null;

        static EffectEditor windows;

        public Dictionary<string, Transform> targetBoneList = new Dictionary<string, Transform>();

        public List<string> bounsName = new List<string>(){"fx_head",
                                "fx_left hand",
                                "fx_right hand",
                                "fx_chest",
                                "fx_left foot",
                                "fx_right foot",
                                "mweapon",
                                "lweapon",
                                "fx_wing",
                                "hand",
        };

        [MenuItem("Plateface/技能编辑")]
        public static void CreateWindow()
        {
            Scene scene = EditorSceneManager.GetActiveScene();
            if (scene.name != "EffectTestScene")
            {
                //EditorSceneManager.OpenScene(DefineSkillEditor.);
            }
            if (windows == null)
            {
                windows = GetWindowWithRect<EffectEditor>(new Rect(0, 0, 1000, 1000), true, "技能编辑", true);
                windows.Show();
            }
        }

        public EffectEditor()
        {
            controlWindow = new PLSkillInfoWindow(this, 0, 0, 300, 100);
            highSkillListWindow = new SkillListWindow(this, 0, 100, 300, 400);
            highSkillListWindow.isHigh = true;
            lowSkillListWindow = new SkillListWindow(this, 0, 500, 300, 400);
            effectListWindow = new EffectListWindow(this, 300, 0, 300, 1000);
            effectInfoWindow = new EffectInfoWindow(this, 600, 0, 400, 1000);

            windowList.Add(controlWindow);
            windowList.Add(highSkillListWindow);
            windowList.Add(lowSkillListWindow);
            windowList.Add(effectListWindow);
            windowList.Add(effectInfoWindow);
        }

        private void OnEnable()
        {
            config = new SkillEffectEditorConfigManager();
            config.LoadByEditor();
            CreateAttackTarget();
        }

        public void CreateAttackTarget()
        {
            Object o = AssetDatabase.LoadAssetAtPath("Assets/Res/Character/Monster/Prefab/monster_hu.prefab", typeof(GameObject));

            if (o == null)
            {
                return;
            }

            attackTarget = Instantiate(o) as GameObject;

            //List<Transform> list = target.transform.GetComponentsInChildren<Transform>(true);
            targetBoneList.Clear();
            targetBoneList.Add("原点", attackTarget.transform);

            // 查询节点
            foreach (var name in bounsName)
            {
                var target2 = attackTarget.transform.RecursionSearch(name);

                if (target2 != null)
                {
                    targetBoneList.Add(name, target2);
                }
            }

            attackTarget.SetActive(false);
        }

        private void OnDestroy()
        {
            if (createdObject != null)
            {
                DestroyImmediate(createdObject);
            }

            if (attackTarget != null)
            {
                DestroyImmediate(attackTarget);
            }

            foreach (var v in effectPlayer)
            {
                v.Release();
            }

            effectBones.Clear();

            if (obj != null)
            {
                obj = null;
            }

            windows = null;
            config.Save();
            //skillData.Save();
            effectListWindow.Release();
        }

        public void OnEvent(IEditorEvent e)
        {
            foreach (var v in windowList)
            {
                v.OnEditorEvent(e);
            }
        }

        public void OnGUI()
        {
            BeginWindows();
            foreach (var v in windowList)
            {
                v.DrawArea();
            }
            EndWindows();
            this.Repaint();
        }


    }
}
